home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / TupleDatabase / TupleDatabase.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  7.3 KB  |  318 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TupleDatabase.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __TUPLEDATABASE__
  15. #include "TupleDatabase.h"
  16. #endif
  17.  
  18. #ifndef    __STRING__
  19. #include "String.h"
  20. #endif
  21.  
  22. #ifndef    __DEBUGASSERT__
  23. #include "DebugAssert.h"
  24. #endif
  25.  
  26. #ifndef    __DEBUGCONSTANTS__
  27. #include "DebugConstants.h"
  28. #endif
  29.  
  30. #pragma segment ATupleDatabase
  31.  
  32. class TDebugFlag;
  33. extern TDebugFlag chrisFlag;
  34.  
  35. /***********************************|****************************************/
  36. /***********************************|****************************************/
  37.  
  38. long
  39. ATupleKey::Compare ( const AComparable& other ) const
  40. {
  41.     const ATupleKey& that = (ATupleKey&) other;
  42.     return ::CompareBuffers ( GetData (), GetLength (), that.GetData (), that.GetLength () );
  43. }
  44.  
  45. /***********************************|****************************************/
  46.  
  47. unsigned long
  48. ATupleKey::SetData ( const char* string )
  49. {
  50.     return SetData ( string, strlen ( string ) );
  51. }
  52.  
  53. /***********************************|****************************************/
  54.  
  55. unsigned long
  56. ATupleKey::SetData ( const void* buffer, unsigned long requestedLength )
  57. {
  58.     unsigned long actualLength = SetLength ( requestedLength );
  59.     unsigned long copiedLength = Minimum ( requestedLength, actualLength );
  60.     BlockMove ( (Ptr) buffer, (Ptr) GetData (), (Size) copiedLength );
  61.     return copiedLength;
  62. }
  63.  
  64. /***********************************|****************************************/
  65.  
  66. #ifndef    __FSTREAM__
  67. #include "FStream.h"
  68. #endif
  69.  
  70. extern ostream& DumpHex (ostream& s, const void* p, unsigned long size);
  71.  
  72. ostream&
  73. ATupleKey::operator >> ( ostream& stream ) const
  74. {
  75.     if ( chrisFlag.Flag ( kExtensiveTupleKeyDescribe ) )
  76.     {
  77.         stream << "ATupleKey @ " << (void*) this << "\n";
  78.         stream << "\tGetLength (): " << GetLength () << "\n";
  79.         stream << "\tGetData (): " << (void*) GetData () << "\n";
  80.         DumpHex ( stream, GetData (), GetLength () );
  81.     }
  82.     else
  83.     {
  84.         stream << "\""; stream.write ( (char*) GetData (), GetLength() ) << "\"";
  85.     }
  86.     
  87.     return stream;
  88. }
  89.  
  90. /***********************************|****************************************/
  91.  
  92. ATupleDatabase::ATupleDatabase()
  93. {
  94. }
  95.  
  96. /***********************************|****************************************/
  97.  
  98. ATupleDatabase::~ATupleDatabase()
  99. {
  100. }
  101.  
  102. /***********************************|****************************************/
  103. /***********************************|****************************************/
  104.  
  105. ATupleKey::ATupleKey ():
  106.     AComparable ()
  107. {
  108. }
  109.  
  110. /***********************************|****************************************/
  111.  
  112. ATupleKey::~ATupleKey ()
  113. {
  114. }
  115.  
  116. /***********************************|****************************************/
  117.  
  118. CFourByteKey::CFourByteKey ():
  119.     ATupleKey (),
  120.     fData ( 0 )
  121. {
  122. }
  123.  
  124. /***********************************|****************************************/
  125.  
  126. CFourByteKey::CFourByteKey ( long value ):
  127.     ATupleKey (),
  128.     fData ( (unsigned long) value )
  129. {
  130. }
  131.  
  132. /***********************************|****************************************/
  133.  
  134. CFourByteKey::CFourByteKey ( unsigned long value ):
  135.     ATupleKey (),
  136.     fData ( value )
  137. {
  138. }
  139.  
  140. /***********************************|****************************************/
  141.  
  142. CFourByteKey::CFourByteKey ( const void* value ):
  143.     ATupleKey (),
  144.     fData ( (unsigned long) value )
  145. {
  146. }
  147.  
  148. /***********************************|****************************************/
  149.  
  150. CFourByteKey::~CFourByteKey ()
  151. {
  152. }
  153.  
  154. /***********************************|****************************************/
  155. /***********************************|****************************************/
  156.  
  157. CTupleKey::CTupleKey ():
  158.     ATupleKey (),
  159.     fBuffer ( 0 )
  160. {
  161.     fBuffer.ZeroBuffer ();
  162. }
  163.  
  164. /***********************************|****************************************/
  165.  
  166. CTupleKey::CTupleKey ( unsigned long length ):
  167.     ATupleKey (),
  168.     fBuffer ( length )
  169. {
  170.     fBuffer.ZeroBuffer ();
  171. }
  172.  
  173. /***********************************|****************************************/
  174.  
  175. CTupleKey::CTupleKey ( const ATupleKey& that ):
  176.     ATupleKey (),
  177.     fBuffer ( that.GetData (), that.GetLength () )
  178. {
  179. }
  180.  
  181. /***********************************|****************************************/
  182.  
  183. CTupleKey::CTupleKey ( const char* string ):
  184.     ATupleKey (),
  185.     fBuffer ( string, strlen ( string ) )
  186. {
  187. }
  188.  
  189. /***********************************|****************************************/
  190.  
  191. CTupleKey::CTupleKey ( const StringPtr string, Boolean includeLengthByte ):
  192.     ATupleKey (),
  193.     fBuffer ( includeLengthByte ? (char*) string : (char*) string + 1, includeLengthByte ? string [ 0 ] + 1 : string [ 0 ] )
  194. {
  195. }
  196.  
  197. /***********************************|****************************************/
  198.  
  199. CTupleKey::CTupleKey ( const void* data, unsigned long length ):
  200.     ATupleKey (),
  201.     fBuffer ( data, length )
  202. {
  203. }
  204.  
  205. /***********************************|****************************************/
  206.  
  207. CTupleKey::~CTupleKey ()
  208. {
  209. }
  210.  
  211. /***********************************|****************************************/
  212. /***********************************|****************************************/
  213.  
  214. unsigned long
  215. CFourByteKey::SetLength ( unsigned long length )
  216. {
  217.     ASSERT ( length == sizeof ( fData ) );
  218.     return sizeof ( fData );
  219. }
  220.  
  221. /***********************************|****************************************/
  222.  
  223. long
  224. CFourByteKey::Compare ( const AComparable& other ) const
  225. {
  226.     ASSERT ( sizeof ( fData ) == ( (const CFourByteKey&) other ).GetLength () );
  227.     return (long) fData - *(long*) ( (const CFourByteKey&) other ).GetData ();
  228. }
  229.  
  230. /***********************************|****************************************/
  231.  
  232. ostream&
  233. CFourByteKey::operator >> ( ostream& s ) const
  234. {
  235.     if ( chrisFlag.Flag ( kExtensiveTupleKeyDescribe ) )
  236.         return ATupleKey::operator >> ( s );
  237.     else
  238.         return s << hexo << fData;
  239. }
  240.  
  241. /***********************************|****************************************/
  242. /***********************************|****************************************/
  243.  
  244. void
  245. ATupleDatabase::Flush ()
  246. {
  247. }
  248.  
  249. /***********************************|****************************************/
  250.  
  251. ostream&
  252. ATupleDatabase::operator >> ( ostream& s ) const
  253. {
  254.     s << "ATupleDatabase @ " << (void*) this << '\n';
  255.     
  256.     unsigned long count = CountTuples ();
  257.     s << "\tCountTuples(): " << count << '\n';
  258.     
  259.     if ( chrisFlag.Flag ( kExtensiveTupleDBDescribe ) )
  260.     {
  261.         CTupleKey key;
  262.         CDataItem data;
  263.         
  264.         for ( unsigned long index = 1; index <= count; index++ )
  265.         {
  266.             if ( ( (ATupleDatabase*) this )->GetTuple ( index, key, data ) )
  267.             {
  268.                 s << key << ", " << data << '\n';
  269.             }
  270.             else
  271.             {
  272.                 s << "\tFailed to GetTuple ( " << index << " )\n";
  273.             }
  274.         }
  275.     }
  276.     
  277.     return s;
  278. }
  279.  
  280. /***********************************|****************************************/
  281.  
  282. Boolean 
  283. ATupleDatabase::GetTuple ( unsigned long index, ATupleKey& key, ADataItem& data ) 
  284. {
  285.     if ( GetTupleKey ( index, key ) )
  286.         return GetTupleData ( key, data );
  287.     else
  288.         return false;
  289. }
  290.  
  291. /***********************************|****************************************/
  292.  
  293. Boolean 
  294. ATupleDatabase::GetTupleData ( unsigned long index, ADataItem& data ) 
  295. {
  296.     CTupleKey key;
  297.     
  298.     if ( GetTupleKey ( index, key ) )
  299.         return GetTupleData ( key, data );
  300.     else
  301.         return false;
  302. }
  303.  
  304. /***********************************|****************************************/
  305.  
  306. Boolean
  307. ATupleDatabase::SetTuple ( unsigned long index, const ADataItem& data ) 
  308. {
  309.     CTupleKey key;
  310.  
  311.     if ( GetTupleKey ( index, key ) )
  312.         return SetTuple ( key, data );
  313.     else
  314.         return false;
  315. }
  316.  
  317. /***********************************|****************************************/
  318.